//
//  M4DecriptionStringView.swift
//  M4
//
//  Created by YuChuang on 2025/1/21.
//

import UIKit
import ActiveLabel

class M4DecriptionStringView: UIView {
    
    @IBInspectable var M4IsCheck: Bool = false
    
    weak var c: UIViewController?
    
    var M4BtnSelectedStatus: ((Bool)->Void)?
    
    lazy var M4Button = {
        let _M4Button = UIButton(type: .custom)
        _M4Button.setImage(UIImage(named: "icon_choose"), for: .normal)
        _M4Button.setImage(UIImage(named: "icon_choose_pre"), for: .selected)
        _M4Button.isSelected = true
        _M4Button.addTarget(self, action: #selector(M4ButtonClick(_:)), for: .touchUpInside)
        self.addSubview(_M4Button)
        _M4Button.snp.makeConstraints { make in
            make.left.equalTo(self)
            make.centerY.equalToSuperview()
            make.height.width.equalTo(18)
        }
        
        return _M4Button
    }()
    
    lazy var M4Label: ActiveLabel = {
       let _M4Label = ActiveLabel()
        _M4Label.font = UIFont.systemFont(ofSize: 12, weight: .regular)
        _M4Label.numberOfLines = 0
        self.addSubview(_M4Label)
        _M4Label.snp.makeConstraints { make in
            make.left.equalTo(M4Button.snp.right).offset(4)
            make.right.top.equalTo(self)
            make.height.greaterThanOrEqualTo(16)
            make.bottom.equalTo(self.snp.bottom).priority(.high)
        }
        
        return _M4Label
    }()

    init(M4IsCheck: Bool=false, c: UIViewController?, M4BtnSelectedStatus: ((Bool)->Void)?) {
        super.init(frame: .zero)
        self.M4IsCheck = M4IsCheck
        self.c = c
        self.M4BtnSelectedStatus = M4BtnSelectedStatus
        M4SetUI()
        configActiveLabel()
    }

    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        M4SetUI()
        configActiveLabel()
    }
    
    func M4SetUI() {
        if M4IsCheck == true {
            M4Button.isHidden = true
            M4Label.snp.updateConstraints { make in
                make.left.equalTo(M4Button.snp.right).offset(-12)
            }
        }
    }
    
    @objc func M4ButtonClick(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        sender.M4EventUnavailable = false
        M4BtnSelectedStatus?(sender.isSelected)
    }
    
    func configActiveLabel() {
        let firstHandle = ActiveType.custom(pattern: "《Contrato de préstamo》")
        let secondHandle = ActiveType.custom(pattern: "《Política de privacidad》")
        M4Label.enabledTypes = [firstHandle, secondHandle]
        if M4IsCheck {
            M4Label.text = "Haga clic para ver el《Contrato de préstamo》y《Política de privacidad》"
        } else {
            M4Label.text = "He leído y estoy de acuerdo《Contrato de préstamo》y《Política de privacidad》"
        }
        
        M4Label.customColor[firstHandle] = COLOR_00479D
        M4Label.customColor[secondHandle] = COLOR_00479D
        M4Label.handleCustomTap(for: firstHandle) { element in
           
            
            if let rootVC = UIApplication.shared.keyWindow?.rootViewController {
                let topVC = rootVC.M4showTopViewController()

                let htmlVC = M4HtmlWebViewController()
                htmlVC.M4WebTitle = "Contrato de préstamo"
                htmlVC.M4WebUrl = M4NetworkAPI.m4htmlPath.rawValue + "/asdasd?vcxoPPo=&sesaPPOs=0"

                topVC.navigationController?.pushViewController(htmlVC, animated: true)
            }
        }
        M4Label.handleCustomTap(for: secondHandle) { element in
            
            if let rootVC = UIApplication.shared.keyWindow?.rootViewController {
                let topVC = rootVC.M4showTopViewController()

                let htmlVC = M4HtmlWebViewController()
                htmlVC.M4WebTitle = "Política de privacidad"
                htmlVC.M4WebUrl = M4NetworkAPI.m4htmlPath.rawValue + "/nafvsdahgv"
                htmlVC.M4OpenPrivacy = true
                topVC.navigationController?.pushViewController(htmlVC, animated: true)
            }
           
        }
    }
}
